Skip to content

fix(ios): join upload revocation claimed by removal cleanup - #294

Merged
sambitcreate merged 2 commits into
mainfrom
fix/ios-upload-revocation-flake
Sep 30, 2026
Merged

sambitcreate merged 2 commits into
mainfrom
fix/ios-upload-revocation-flake

Conversation

@sambitcreate

Copy link
Copy Markdown
Owner

Summary

testUploadCleanupRevocationDoesNotDeadlockRemoval and testUploadPostRevocationCompletesPurgeWithoutSelfAwait failed together in the iOS simulator job. Failing run: https://github.com/sambitcreate/aiden-agent/actions/runs/36295088605. Both passed on a later fresh run.

Root cause

When an attachment cleanup DELETE returns credential_revoked, AidenChatViewModel defers the credential revocation. Two paths could claim it:

  • upload() awaited handleCredentialRevocation. That was correct.
  • Lifetime removal cleanup (cleanupOwnedAttachmentsForRemoval, which AidenChatCache.purge/removeChat join) must not await the purge that joins it. It started an unobserved, detached Task instead.

When removal cleanup claimed the revocation first, upload() returned before the purge had run. That purge then ran during the next test and competed with it for the shared stores (bot cache, draft store, model preferences, chat cache, Live Activities). Instrumentation confirmed that the first test's purge ran inside the second test's time window.

The hosted runner also had multi-second stalls in this run: testMarkdownView took 66s against 0.04s locally. With the leaked purge added, the 2–3s fulfillment windows were exceeded, and the two tests failed as a pair. The detached task is the defect we can fix. The host stalls made it visible.

Fix

  • The deferred revocation now lives in one owned uploadRevocationTask.
  • Removal cleanup starts that task but never awaits it, so the purge still never waits on itself.
  • upload() and removeAttachment join the task, so they return only after the revocation and purge have finished.
  • No timeouts, retries, sleeps or skips were added, and no assertions were weakened.

Test

  • In the revoked case, assertRemovalJoinsUploadCleanup now asserts coordinator.connectionState == .needsPairing once the upload returns. The coordinator sets that state only after the purge finishes.
  • The assertion fails deterministically without the fix ("connected" is not equal to "needsPairing", on the first iteration) and passes with it.

Validation (local, Xcode 27 beta, iPhone 17 Pro simulator)

  • The two tests passed 50 iterations with -run-tests-until-failure.
  • The full AidenChatTests class passed 3 iterations with -run-tests-until-failure.

Android

This is iOS client concurrency only. No shared protocol, server contract or transcript UI changed, so Android needs no change.

🤖 Generated with Claude Code

When a chat's lifetime removal cleanup claimed an attachment DELETE's
credential revocation, it ran it in a detached, unobserved Task. The
upload then returned before the installation purge ran, and that purge
leaked into whatever ran next. In hosted CI it overlapped the following
upload revocation test and, together with simulator stalls, timed both
out (run 36295088605).

The revocation now lives in one owned task. Removal cleanup may start
it but never awaits it, which avoids a purge waiting on itself. Upload
and removeAttachment join it, so they return only after the purge. The
removal test now checks that the coordinator reads needsPairing once
the upload returns. The check fails deterministically without the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@very-hermes-bot

very-hermes-bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Hermes Review Bot

Confidence: 5

Engine: agy/gemini-3.8-flash-high
Review mode: small
Head: 94157868b98b682590b855053609f6660f8be33b
Generated: 2026-09-30T19:23:32+00:00
Reviews: 1

Summary

Resolves a flaky test failure in the iOS test suite (testUploadCleanupRevocationDoesNotDeadlockRemoval and testUploadPostRevocationCompletesPurgeWithoutSelfAwait) by synchronizing callers with deferred credential revocation handling. Previously, lifetime removal cleanup claimed the deferred revocation from a failed attachment cleanup DELETE and spawned an unobserved, detached Task to avoid self-await deadlocks during cache purge, causing upload() to return before the installation purge completed and allowing the unobserved purge to race against subsequent test executions. The fix tracks deferred revocation handling in an owned uploadRevocationTask that removal cleanup initiates without awaiting, while upload() and removeAttachment explicitly join it before returning. Maintainers should double-check that joinDeferredUploadRevocation() properly clears uploadRevocationTask only when the completed task matches the observed reference, ensuring chained revocations or subsequent cleanup tasks are not prematurely dropped.

Confidence Score: 5/5

Fully traced end-to-end through AidenChatViewModel's upload lifecycle, lifetime removal cleanup paths, cancellation handlers, and the updated AidenChatTests assertions.

📁 Important Files Changed
  • ios/AidenOnTheGo/Features/Remote/AidenChatFeature.swift: Introduces uploadRevocationTask, startDeferredUploadRevocation(), and joinDeferredUploadRevocation() to synchronize callers with deferred credential revocation handling without reintroducing purge self-await deadlocks.
  • ios/AidenOnTheGoTests/AidenChatTests.swift: Hooks the coordinator in assertRemovalJoinsUploadCleanup and asserts coordinator.connectionState == .needsPairing upon upload return to verify the purge completes before upload finishes.
  • .memory/durable-attachment-lifecycle.md: Documents the deferred revocation task ownership model and root cause context.
  • ios/CHANGELOG.md: Records the user-facing release note for upload completion post-revocation.

Findings

No findings.

📊 Sequence Diagram
sequenceDiagram
    autonumber
    actor Caller as upload()
    participant VM as AidenChatViewModel
    participant Server as Remote Server
    participant Lifetime as onRemovalCleanup
    participant Task as uploadRevocationTask
    participant Coord as Coordinator

    Caller->>VM: upload([attachment])
    Lifetime->>VM: removeChat() / purge() cancels uploadTask
    VM->>Server: DELETE /attachments/{id}
    Server-->>VM: 401 credential_revoked
    VM->>VM: deferUploadRevocation() records (error, context)
    Lifetime->>VM: cleanupOwnedAttachmentsForRemoval()
    VM->>Task: startDeferredUploadRevocation() (unawaited by cleanup)
    Task->>Coord: handleCredentialRevocation(error, context)
    Lifetime-->>Caller: removal cleanup finishes without deadlock
    Caller->>VM: joinDeferredUploadRevocation()
    VM->>Task: await revocation.value
    Coord-->>Task: installation purge completes (needsPairing)
    Task-->>VM: task finished
    VM-->>Caller: upload returns after purge completes
Loading

Machine-Readable Findings

[]

Last reviewed commit: 94157868b98b
Reviews (1) · Comment /hermes review to trigger a new review · /hermes review full for full re-review

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

A subsequent revocation can replace the task handle while its purge is running, allowing a later joiner to return before installation data is purged.

Reviewed changes Reviewed the iOS deferred upload-revocation change and its lifecycle cleanup, regression assertion, and release/project notes.

  • Owned revocation task. Deferred attachment-cleanup revocation is stored in one task; removal cleanup starts it without awaiting the purge it joins, while upload and explicit removal await it.
  • Regression assertion. The removal/upload test checks that the coordinator reaches .needsPairing when upload returns.
  • Documentation. The iOS changelog and attachment lifecycle memory note record the new completion behavior.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using GPT Luna | 𝕏

A second cleanup revocation that arrived during an earlier purge replaced
the task handle. Its coordinator call returns at once for the now-stale
context, so a later joiner could return before the first purge finished.
The replacement now awaits the previous task first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — one regression-coverage suggestion inline.

Reviewed changes Reviewed the follow-up that serializes deferred upload revocation behind an installation purge already in progress.

  • Chained revocation tasks. Captured the current revocation task and awaited it before dispatching a replacement to the coordinator.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using GPT Luna | 𝕏

// the replacement must still finish only after the earlier purge.
let previous = uploadRevocationTask
uploadRevocationTask = Task {
await previous?.value

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current tests exercise only one revoked cleanup, so they would still pass if this wait were removed. Since the second-revocation-during-purge interleaving is the exact race this change prevents, please add a deterministic regression case that holds the first purge, triggers another revoked cleanup, and verifies a joiner cannot finish until the first purge completes.

Technical details
# Cover replacement revocation ordering

## Affected sites
- `ios/AidenOnTheGo/Features/Remote/AidenChatFeature.swift:2286` waits for earlier purge-owning tasks.
- `ios/AidenOnTheGoTests/AidenChatTests.swift:2731-2795` exercises a single revoked DELETE; `2797-2820` covers POST revocation, not overlapping cleanup revocations.

## Required outcome
- A test must fail if the replacement task no longer awaits the prior task, using a controlled in-flight purge and a subsequent `credential_revoked` cleanup response.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged. A deterministic test would need to hold the first purge. The purge runs through purgeInstallationData, which touches shared singletons (the bot cache, draft store and Live Activities) and has no test seam. Adding that seam is a larger test-infrastructure change than this flake fix. I'm deferring it as a follow-up rather than adding a timing-dependent test.

@sambitcreate
sambitcreate merged commit c65acd1 into main Sep 30, 2026
23 checks passed
@sambitcreate
sambitcreate deleted the fix/ios-upload-revocation-flake branch September 30, 2026 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants